package NG::Sys::Data;
use strict;
use warnings;

require Exporter;
our @ISA = qw(Exporter);
our @EXPORT = qw(
array_to_hash
hash_to_array
copy_hash
);

sub array_to_hash {
	my $hash = shift;
	my $array = shift;

	my $key;
	unless($hash){
		return {};
	}elsif(!$array || ref $array){
		$key = shift;
	}else{
		$key = $array;
		$array = $hash;
		$hash = {};
	}

	if($array){
		my $s = @_ == 1 ? shift : undef;
		unless(UNIVERSAL::isa($array,"ARRAY")){
			$array = [ $array ];
		}
		for(@$array){
			$hash->{$_->{$key}} = $s ? $_->{$s} : [@{$_}{@_}];
		}
	}
	return $hash;
}
sub hash_to_array {
	my $hash = shift;
	my $key = shift;
	my $val = shift;
	my @result;
	while(my($k,$v) = each %$hash){
		push @result,{
					$key => $k,
					$val => $v,
					};
	}
	return \@result;
}
sub copy_hash {
	my $hash = shift;
	my @keys = @_;
	unless(@keys){
		@keys = keys %$hash;
	}
	my %ret;
	@ret{@keys} = @{$hash}{@keys};
	return \%ret;
}
1;
